package com.jk.dmconsumer.utils;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


/**
 *
 * 类: HttpClient <br>
 * 描述: httpclient工具类 <br>
 * 作者: song<br>
 * 时间: 2017年7月21日 下午3:15:27
 */
public class HttpClientUtil {

	static CloseableHttpClient client = null;
	static {
		//1、创建httpClient实例
		client = HttpClients.createDefault();
	}

	/**
	 *
	 * 方法: get <br>
	 * 描述: get请求 <br>
	 * 作者: Teacher song<br>
	 * 时间: 2017年7月21日 下午3:15:25
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */



	public static String post2(String url,HashMap<String, Object> params,HashMap<String, Object> paramsHeader) {
		try {
			//2、创建post请求对象
			HttpPost httpPost = new HttpPost();
			//设置路径
			httpPost.setURI(new URI(url));

			//3、给header设置参数
			Set<String> keySet2 = paramsHeader.keySet();
			for (String key : keySet2) {
				String value = paramsHeader.get(key).toString();
				httpPost.addHeader(key, value);
			}

			//4、设置post请求参数
			//a、hashmap的遍历：获取里面的key和value
			//b、拼接list
			List<NameValuePair> parameters = new ArrayList<NameValuePair>();
			Set<String> keySet = params.keySet();
			for (String key : keySet) {
				NameValuePair e = new BasicNameValuePair(key, params.get(key).toString());
				parameters.add(e);
			}

			HttpEntity entity = new UrlEncodedFormEntity(parameters , "utf-8");
			httpPost.setEntity(entity);

			//5、执行：发送请求
			CloseableHttpResponse execute = client.execute(httpPost);
			//6、获取请求的状态码
			int statusCode = execute.getStatusLine().getStatusCode();
			if (200 != statusCode) {
				return "";
			}
			return EntityUtils.toString(execute.getEntity(), "utf-8");
		}catch (Exception e) {
			//6、关闭连接
			try {
				client.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
			return null;
		}
	}



	//post请求 ：调用发送短信验证码的接口
	public static String Yzm(String args,HttpServletRequest request) {
		String url = "https://api.netease.im/sms/sendcode.action";
		HashMap<String, Object> params = new HashMap<String, Object>();
		HashMap<String, Object> paramsHeader = new HashMap<String, Object>();

		//一、处理header参数
		//1、AppKey 网抑云信账号

		//paramsHeader.put("AppKey", "ddaa74e4f1fb7138613c296396dce5f7");

		//2、随机数6位  100000 999999
		long nonce = Math.round(((Math.random()*899999+100000)));
		paramsHeader.put("Nonce", nonce);
		//3、当前UTC时间戳，从1970年1月1日0点0 分0 秒开始到现在的秒数(String) //转换秒数
//			 String curTime = String.valueOf((new Date()).getTime() / 1000L);第二种方式
		long curTime = System.currentTimeMillis()/1000;
		paramsHeader.put("CurTime", curTime);
		//4、checkSum     网抑云信秘钥
		String appSecret = "bc2b9982ef5d";
		String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce+"", curTime+"");
		paramsHeader.put("CheckSum", checkSum);
		//将手机号变为活值
		String attribute = (String) request.getSession().getAttribute("useriPhone");
		//二、处理body参数                   手机号
		params.put("mobile", attribute);//"16619878719"
		//   短信模板ID
		params.put("templateid", "19487878");
		//随机数
		params.put("authCode", nonce);//随机验证码
		request.getSession().setAttribute("checkcode",nonce);//将随机生成的验证码存到session中
		String result = HttpClientUtil.post2(url, params, paramsHeader);
		System.out.println(result);
		return result;
	}
}
